#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "common.h"
#include <string.h>
#include <stdlib.h>
#define DEV STR_DEV
// cameraID=0,x=22,y=34,enable=1,str=字符串
void test_string(int camera_id, char *dst_buf, int dst_buf_len, char *string, int x, int y);
char wmark_str[256] = {"cameraID=0,x=22,y=34,enable=1,str=2022-01-05 16:46:43 180(km/h)"};
int main(int argc, char **argv)
{
    int fd;
    char num[3];
    int i = 0;
    int enable = 1;
    int cameraId = 2;
    int x = 0 , y = 0;

    if (argc < 2)
    {
        printf("help:\n\t%s cameraId\n", argv[0]);
        printf("说明:\n\t %s 2 : 给第一个摄像头 加水印\n", argv[0]);
    }
    if (argc >= 2)
    {
        cameraId = atoi(argv[1]);
        if (cameraId < 2)
        {
            printf("请输入 cameraId >= 2");
            cameraId = 2;
        }
    }
    printf("cameraId = %d\n", cameraId);
    fd = open(DEV, O_RDWR);
    if (fd == -1)
    {
        printf("device open failure\n");
        return -1;
    }
    while (1)
    {
        test_string(cameraId, wmark_str, sizeof(wmark_str), "你好", x, y);
        write(fd, wmark_str, sizeof(wmark_str));


        usleep(500000);

        test_string(cameraId, wmark_str, sizeof(wmark_str), "hello world, 12345678", x, y);
        write(fd, wmark_str, sizeof(wmark_str));

        usleep(500000);

        x += 10;
        y += 5;
        if (x > 1280) {
            x = 0;
        }

        if (y > 960) {
            y = 0;
        }
    }
    close(fd);
    return 0;
}

void test_string(int camera_id, char *dst_buf, int dst_buf_len, char *string, int x, int y)
{
    static int count = 0;
    memset(dst_buf, 0, dst_buf_len);
    sprintf(dst_buf, "cameraID=%d,x=%d,y=%d,enable=1,str= %s %d-%d-%d-%d", camera_id, x, y, string, count, count, count, count);
    count++;
    
    if (count > 200) {
        count = 0;
    }
}
